const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 获取当前页面的url
 */
function getcurrentPageUrl () {
  let pages = getCurrentPages()
  let currentPage = pages[pages.length - 1]
  let url = currentPage.route // 当前页面url
  return url
}
/**
 * 获取自定义tabBar当前页面的索引
 * params tabBarList 根据传进来的url列表，判断当前页面的索引
 */
function getTabBarIndex(tabBarList) {
  let url = '/' + getcurrentPageUrl()
  let index = tabBarList.findIndex(item => item.pagePath == url)
  if (index < 0) index = 0
  return index
}

module.exports = {
  formatTime: formatTime,
  getTabBarIndex: getTabBarIndex
}
